<?php
/**
 * FTP Class
 * @TODO batch process tasks
 * @author	zhangjian<smile_for_life@sina.cn>
 * @date	2014-01-14
 */
 class Ftp
 {
	const FTP_SERVER_HOST = 'ftp_server_host';
	const FTP_SERVER_PORT = 'ftp_server_port';
	const FTP_AUTH_USERNAME = 'ftp_auth_username';
	const FTP_AUTH_PASSWORD = 'ftp_auth_password';
	const FTP_CONNECT_TIMEOUT = 'ftp_connect_timeout';
	const FTP_DOWNLOAD_HISTORY_FILE = 'ftp_download_history_file';
	
	protected $_conn = null;
	protected $_config = array();
	protected $_default_config = array(
		'ftp_server_port'=>21,
		'ftp_connect_timeout'=>60
	);
	
	public function __construct($ftp_config = array())
	{
		if(is_array($ftp_config) && $ftp_config){
			$this->init($ftp_config);
		}
	}
	
	public function __destruct()
	{
		if($this->_conn){
			ftp_close($this->_conn);
		}
	}
	
	/**
	 * get file list from ftp server
	 */
	public function get_list($dir_remote = '', $is_raw = false)
	{
		$return = false;

		if($dir_remote){
			$this->_conn = ftp_connect($this->_config[self::FTP_SERVER_HOST], $this->_config[self::FTP_SERVER_PORT], $this->_config[self::FTP_CONNECT_TIMEOUT]);
			try{
				if($this->_conn && ftp_login($this->_conn, $this->_config[self::FTP_AUTH_USERNAME], $this->_config[self::FTP_AUTH_PASSWORD])){
					ftp_pasv($this->_conn, true);
					
					if($is_raw){
						$return = ftp_rawlist($this->_conn, $dir_remote);
					}else{
						$return = ftp_nlist($this->_conn, $dir_remote);
						if(is_file($this->_config[self::FTP_DOWNLOAD_HISTORY_FILE])){
							$history = file($this->_config[self::FTP_DOWNLOAD_HISTORY_FILE], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
						}

						if(is_array($history) && $history){
							$return = $history;//array_diff($return, $history);
						}
					}
				}
			}catch(Exception $e){
				$return = false;
			}
		}
		return $return;
	}
	
	/**
	 * get file from ftp server
	 */
	public function get_file($remote_file = '', $local_file = '')
	{
		$return = false;
		
		if($this->_conn && $remote_file && $local_file){
			$return = ftp_get($this->_conn, $local_file, $remote_file,FTP_BINARY);
			if($return){
				$log = basename($remote_file).PHP_EOL;
				file_put_contents($this->_config[self::FTP_DOWNLOAD_HISTORY_FILE],$log,FILE_APPEND);
			}
		}
		
		return $return;
	}
	
	/**
	 * initialize ftp connect,config ftp server
	 */
	 protected function init($ftp_config = array())
	{
		$return = false;
		if($this->init_config($ftp_config)){
			$this->_config = $ftp_config;
			$return = true;
		}
		
		return $return;
	}
	
	/**
	 * initialize configure infomation
	 */
	protected function init_config(&$ftp_config = array())
	{
		$return = true;
		
		if(is_array($ftp_config) && $ftp_config){
			$ftp_config = array_merge($this->_default_config, $ftp_config);

			foreach($ftp_config as $key => $item){
				switch(true){
					case self::FTP_SERVER_HOST == $key:
					case self::FTP_SERVER_PORT == $key:
					case self::FTP_AUTH_USERNAME == $key:
					case self::FTP_AUTH_PASSWORD == $key:
					case self::FTP_CONNECT_TIMEOUT == $key:
					case self::FTP_DOWNLOAD_HISTORY_FILE == $key:
						$return = $return && $item;
					break;
					default:
						unset($ftp_config[$key]);
				}
			}
		}

		return $return;
	}
 }